/**
 * 客户设备档案信息维护 - 编辑页面
 * @author miaoyl
 * @date 2018/01/17
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        vueData = {
            isAdd: false,
            form: {
                MMRA042_NAME: null
            },
            rules: {
                MMRA001: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                MMRA002: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                MMRA004: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                MMRA006: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                MMRA003: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                MMRA009: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                MMRA010: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                MMRA011: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
               /* MMRA008: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],*/
                MMRA013: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                MMRA012: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                /*MMRA044: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],*/
                MMRA017_NAME: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                MMRAACTI: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ]
            },
            selectOptions:{
                sys_y_n:app.datas_("sys_y_n"),
                sys_1_0:app.datas_("sys_1_0"),
            },
        },
        callObj,
        pageVue
    ;

    var Gwai610Edit = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwai610_edit',
                _loadTplsFull: [/*{
                        dom: 'common_open_kh', tpl: 'common/_open_kh'
                    },
                    {
                        dom: 'common_open_cp', tpl: 'common/_open_cp'
                    },
                    {
                        dom: 'common_open_gs', tpl: 'common/_open_gs'
                    },
                    {
                        dom: 'common_open_xm', tpl: 'common/_open_xm'
                    },*/
                    {
                        dom: 'common_open_window', tpl: 'common/_open_window'
                    }
                ]
            }, win);
        },

        editData: function () {
            app.ajax({
                xurl: 'crm/gwai610/edit',
                xdata: vueData.form,
                loading: true,
                okmsg: '客户设备档案信息修改成功',
                cb: function (result) {
                    if (result.ok) {
                        //同步更新缓存对应的值
                        callObj.update(result.xdata);
                        layer.close(callObj.layOpenIndex);
                    }
                }
            });
        },

        addData: function () {
            app.ajax({
                xurl: 'crm/gwai610/add',
                xdata: vueData.form,
                loading: true,
                okmsg: '客户设备档案信息保存成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule('gwai610').reloadTableCurr();
                        layer.close(callObj.layOpenIndex);
                    }
                }
            });
        },

        _call: function (obj) {
            pageVue.$refs['form'].resetFields();
            callObj = obj;
            if (obj.is_add) {
                vueData.isAdd=true;
                // app.log(obj);
                app.extendUpLow({
                    target: vueData.form,
                    res: {
                        MMRAACTI:'Y'
                    },
                    reset: true
                });
            } else {
                vueData.isAdd = false;
                app.extendUpLow({
                    target: vueData.form,
                    res: [callObj.data,{
                        oldMMRA001:callObj.data.MMRA001,
                        oldMMRA002:callObj.data.MMRA002,
                        oldMMRA003:callObj.data.MMRA003,
                    }],
                    reset: true
                });
            }

        },
        _ready: function () {
            vueData.form = app.fields_(['crm_gwai610'], vueData.form);
            pageVue = new Vue({
                el: '#gwai610_edit',
                data: vueData,
                watch:{
                    'form.MMRA042_NAME':function () {
                        if(vueData.form.MMRA042_NAME == ''){
                            vueData.form.MMRA042 = '';
                            vueData.form.MMRA043 = '';
                        }
                    },
                    'form.MMRA010': function () {
                        if(vueData.form.MMRA010 != '' && vueData.form.MMRA010 != null){
                            app.ajax({
                                xurl: 'crm/gwai610/calcuThreeDate',
                                xdata: {MMRA002: vueData.form.MMRA002,
                                    MMRA010: vueData.form.MMRA010
                                },
                                cb: function (result) {
                                    if (result.ok) {
                                        vueData.form.MMRA012 = result.data.data.threeDate;
                                    }
                                }
                            });
                        }
                    }
                },
                methods: {
                    cancel: function () {
                        layer.close(callObj.layOpenIndex);
                    },
                    sure: function () {
                        this.$refs['form'].validate(function (valid) {
                            if (valid) {
                                if (vueData.isAdd) {
                                    // if (pageVue.$refs.upload1.uploadFiles.length > 0) {
                                    //     pageVue.$refs.upload1.submit();
                                    // }else {
                                    //     self.addData();
                                    // }
                                    self.addData();
                                } else {
                                    // if (pageVue.$refs.upload1.uploadFiles.length > 0) {
                                    //     pageVue.$refs.upload1.submit();
                                    // }else {
                                    //     self.editData();
                                    // }
                                    self.editData();
                                }
                            }
                        });
                    },
                    beforeUploadImg: function (file) {
                        const isJPG = (file.type === 'image/png' || file.type === 'image/jpeg'
                        || file.type === 'image/gif');
                        const isLt2M = file.size / 1024 / 1024 < 2;
                        if (!isJPG) {
                            this.$message.error("上传图片只能是 JPG|PNG|GIF 格式!");
                        }
                        if (!isLt2M) {
                            this.$message.error("上传图片大小不能超过 2MB!");
                        }

                        return isJPG && isLt2M;
                    },
                    successUploadImg: function (response) {
                        if (response.code == 200) {
                            vue.$message.success('图片上传成功');
                            vueData.form.MMRA019 = response.data[0].name;
                            if (vueData.isAdd) {
                                self.addData();
                            }else {
                                self.editData();
                            }
                        }
                    },
                    handleRemove: function (file, fileList) {
                        vueData.form.MMRA019 = "";
                    },
                    handlePreview: function (file) {
                    },
                    handleExceedImg: function (files, fileList) {
                        this.$message.warning("当前只允许选择一个图片，如果需要选择新的图片，请先删除之前的图片!");
                    },
                    openKh:function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'q_gwak001',
                                    columnList: [
                                        {label: '客户编号', prop: 'GWAK001', width: '150'},
                                        {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                        {label: '客户全称', prop: 'GWAKL004', width: '150'}
                                    ]
                                },
                                cb: function (data) {
                                    vueData.form.MMRA001 = data.GWAK001;
                                    vueData.form.GWAKL004 = data.GWAKL004;
                                    vueData.form.GWAK031 = data.GWAK031;
                                    vueData.form.GWAK030 = data.GWAK030;
                                }
                            },
                            index:0,
                            openOpts: {
                                title: '选择客户',
                                area:['600px', '470px']
                            }
                        });
                    },
                    clearMmra001:function (){
                        vueData.form.GWAKL004 = '';
                        vueData.form.GWAK031 = '';
                        vueData.form.GWAK030 = '';
                    },
                    openCp:function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'q_gwam001',
                                    columnList: [
                                        {label: '产品编号', prop: 'GWAM001', width: '150'},
                                        {label: '品名', prop: 'GWAML003', width: '150'},
                                        {label: '规格', prop: 'GWAML004', width: '150'}
                                    ]
                                },
                                cb: function (data) {
                                    vueData.form.MMRA002 = data.GWAM001;
                                    vueData.form.MMRA004 = data.GWAML003;
                                    vueData.form.MMRA006 = data.GWAML004;
                                    vueData.form.MMRA003 = data.GWAM001;
                                }
                            },
                            index:0,
                            openOpts: {
                                title: '选择产品',
                                area:['600px', '470px']
                            }
                        });
                    },
                    clearMmra002:function () {
                        vueData.form.MMRA004 = '';
                        vueData.form.MMRA006 = '';
                        vueData.form.MMRA003 = '';
                    },
                    openMmra017:function () {//设备销售方开窗
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'q_sysorg_biz',
                                    columnList: [{label: 'ID', prop: 'ID', width: '150'},
                                        {label: '公司名称', prop: 'NAME', width: '150'}
                                    ]
                                },
                                cb: function (data) {
                                    vueData.form.MMRA017 = data.ID;
                                    vueData.form.MMRA017_NAME = data.NAME;
                                }
                            },
                            index:0,
                            openOpts: {
                                title: '选择设备销售方',
                                area:['600px', '470px']
                            }
                        });
                    },
                    openXm:function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    columnList: [{label: '项目编号', prop: 'GWEM001', width: '150'},
                                        {label: '项目名称', prop: 'GWEM002', width: '150'}],
                                    url: 'crm/gwai610/listGwemPage',
                                },
                                cb: function (data) {
                                    vueData.form.MMRA044 = data.GWEM001;
                                }
                            },
                            index: 4,
                            openOpts: {
                                title: '选择项目编号',
                                area: ['600px', '465px']
                            }
                        });
                    },
                    openMmra042:function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'q_sysuser_id',
                                    columnList: [{label: 'ID', prop: 'ID', width: '150'},
                                        {label: '名称', prop: 'NAME', width: '150'}]
                                },
                                cb: function (data) {
                                    vueData.form.MMRA042 = data.ID;
                                    vueData.form.MMRA042_NAME = data.NAME;
                                    vueData.form.MMRA043 = data.MOBILE;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择业务员',
                                area: ['600px', '465px']
                            }
                        });
                    }
                },
                created: function () {
                    self._setEleIsReady('gwai610_edit');
                }
            });
        },

        _lazy: function () {
            this._super();
        }
    });

    return new Gwai610Edit();

});
